package com.iyue.user;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.iyue.SpringHelper;
import com.iyue.activity.helper.ActivityFunctions;
import com.iyue.activity.pojo.ActivityCategory;
import com.iyue.framework.BaseAction;
import com.iyue.servlet.taglibs.Functions;
import com.iyue.user.helper.UserUtil;
import com.iyue.user.pojo.UserSubscript;
import com.iyue.user.service.UserBiz;

/**
 * 《网站注册》实现类
 * 
 * @author 孙晓晔
 * @version $Id: Reg.java,v 1.1 2010/10/14 17:07:17 fresh Exp $
 */
public class Subscript extends UserUtil {

	private static final Integer USER_SUBSCRIPT_LIMIT = 5;

	public ActionForward layout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		if (!isLogin(request)) {
			return action.display(request, response, "login");
		}

		Integer username = u(request);
		UserBiz biz = (UserBiz) SpringHelper.getBean("UserBiz");
		List<Integer> result = biz.getSubscript(username);
		if (result != null && result.size() > 0) {
			request.setAttribute("result", result);
		}

		return action.display(request, response, "subscript/layout");
	}

	/**
	 * 关注（选择分类）
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param action
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		if (!isLogin(request)) {
			return action.display(request, response, "login");
		}

		List<ActivityCategory> list = ActivityFunctions.getCategories(1);
		if (list == null || list.isEmpty()) {
			request.setAttribute("msg", "系统异常，请稍后再试！");
			return action.display(request, response, "info");
		}

		request.setAttribute("result", list);

		return action.display(request, response, "subscript/add");
	}

	/**
	 * 添加关注
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param action
	 * @return
	 * @throws Exception
	 */
	public ActionForward addPost(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		if (!isLogin(request)) {
			return action.display(request, response, "login");
		}

		String backurl = request.getParameter("go");
		if (StringUtils.isBlank(backurl)) {
			backurl = "/user.do?do=Subscript.layout";
		}

		String msg = "";
		String cid = request.getParameter("cid");
		if (StringUtils.isBlank(cid)) {
			msg = "参数错误！";
			Redirect(request, response, backurl + "&msg="
					+ Functions.encode(msg));
			return null;
		}

		Integer sid = new Integer(cid);
		ActivityCategory ac = ActivityFunctions.category(sid);
		if (ac == null) {
			msg = "参数错误！";
			Redirect(request, response, backurl + "&msg="
					+ Functions.encode(msg));
			return null;
		}

		request.setAttribute("cid", cid);

		List<ActivityCategory> list = ActivityFunctions.subs(sid);
		if (list != null && list.size() > 0) {
			request.setAttribute("result", list);
			return action.display(request, response, "subscript/category");
		}

		Integer username = u(request);
		UserBiz biz = (UserBiz) SpringHelper.getBean("UserBiz");
		Integer count = biz.getSubscript_count(username);
		if (count >= USER_SUBSCRIPT_LIMIT) {
			msg = "您关注已经超过限制了！";
			Redirect(request, response, backurl + "&msg="
					+ Functions.encode(msg));
			return null;
		}

		UserSubscript us = new UserSubscript();
		us.setUsername(username);
		us.setSid(sid);
		us.setStatus(UserSubscript.STATUS_OK);
		us.setCreatetime(new Date());
		int ret = biz.update_subscript(us);
		if (ret <= 0) {
			ret = biz.save_subscript(us);
		}

		if (ret > 0) {
			msg = "关注设置成功";
		} else {
			msg = "关注设置失败";
		}

		Redirect(request, response, backurl + "&msg=" + Functions.encode(msg));
		return null;
	}
	public ActionForward addPost2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		if (!isLogin(request)) {
			return action.display(request, response, "login");
		}		 

		String msg = "";
		String cid = request.getParameter("cid");
		if (StringUtils.isBlank(cid)) {
			msg = "参数错误！";
			response.getWriter().write(msg);
			return null;
		}

		Integer sid = new Integer(cid);
		ActivityCategory ac = ActivityFunctions.category(sid);
		if (ac == null) {
			msg = "参数错误！";
			response.getWriter().write(msg);
			return null;
		}

		request.setAttribute("cid", cid);

		 

		Integer username = u(request);
		UserBiz biz = (UserBiz) SpringHelper.getBean("UserBiz");
		Integer count = biz.getSubscript_count(username);
		if (count >= USER_SUBSCRIPT_LIMIT) {
			msg = "您关注已经超过限制了！";
			response.getWriter().write(msg);
			return null;
		}

		UserSubscript us = new UserSubscript();
		us.setUsername(username);
		us.setSid(sid);
		us.setStatus(UserSubscript.STATUS_OK);
		us.setCreatetime(new Date());
		int ret = biz.update_subscript(us);
		if (ret <= 0) {
			ret = biz.save_subscript(us);
		}

		if (ret > 0) {
			msg = "关注设置成功";
		} else {
			msg = "关注设置失败";
		}

		response.getWriter().write(msg);
		return null;
	}
	public ActionForward addPost3(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		if (!isLogin(request)) {
			return action.display(request, response, "login");
		}
		String msg = "";
		String cid = request.getParameter("cid");
		if (StringUtils.isBlank(cid)) {
			msg = "参数错误！";
			response.getWriter().write(msg);
			return null;
		}

		Integer sid = new Integer(cid);
		ActivityCategory ac = ActivityFunctions.category(sid);
		if (ac == null) {
			msg = "参数错误！";
			response.getWriter().write(msg);
			return null;
		}

		request.setAttribute("cid", cid);

		List<ActivityCategory> list = ActivityFunctions.subs(sid);
		if (list != null && list.size() > 0) {
			request.setAttribute("result", list);
			return action.display(request, response, "subscript/category");
		}

		Integer username = u(request);
		UserBiz biz = (UserBiz) SpringHelper.getBean("UserBiz");
		Integer count = biz.getSubscript_count(username);
		if (count >= USER_SUBSCRIPT_LIMIT) {
			msg = "您关注已经超过限制了！";
			response.getWriter().write(msg); 
			return null;
		}

		UserSubscript us = new UserSubscript();
		us.setUsername(username);
		us.setSid(sid);
		us.setStatus(UserSubscript.STATUS_OK);
		us.setCreatetime(new Date());
		int ret = biz.update_subscript(us);
		if (ret <= 0) {
			ret = biz.save_subscript(us);
		}

		if (ret > 0) {
			msg = "关注设置成功";
		} else {
			msg = "关注设置失败";
		}

		response.getWriter().write(msg); 
		return null;
	}

	/**
	 * 删除关注
	 */
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		if (!isLogin(request)) {
			return action.display(request, response, "login");
		}

		String backurl = request.getParameter("go");
		if (StringUtils.isBlank(backurl)) {
			backurl = "/user.do?do=Subscript.layout";
		}

		String msg = "";
		String cid = request.getParameter("cid");
		if (StringUtils.isBlank(cid)) {
			msg = "参数错误！";
			Redirect(request, response, backurl + "&msg="
					+ Functions.encode(msg));
			return null;
		}

		UserBiz biz = (UserBiz) SpringHelper.getBean("UserBiz");
		Integer username = u(request);
		int ret = biz.delete_subscript(username, new Integer(cid));
		if (ret > 0) {
			msg = "操作成功";
		} else {
			msg = "操作失败";
		}

		Redirect(request, response, backurl + "&msg=" + Functions.encode(msg));
		return null;
	}
	public ActionForward delete2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			BaseAction action) throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		if (!isLogin(request)) {
			return action.display(request, response, "login");
		} 

		String msg = "";
		String cid = request.getParameter("cid");
		if (StringUtils.isBlank(cid)) {
			msg = "参数错误！";
			response.getWriter().write(msg); 
			return null;
		}

		UserBiz biz = (UserBiz) SpringHelper.getBean("UserBiz");
		Integer username = u(request);
		int ret = biz.delete_subscript(username, new Integer(cid));
		if (ret > 0) {
			msg = "操作成功";
		} else {
			msg = "操作失败";
		}

		response.getWriter().write(msg);
		return null;
	}


}
